from django.db import models

from django.db import models


# 基类
class BaseModel(models.Model):
    # 创建时间
    create_time = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)

    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:

        abstract = True


# 部门表
class DeptModel(BaseModel):
    name = models.CharField("部门名称", max_length=50)
    remarks = models.CharField("备注", max_length=256)

    class Meta:
        db_table = "dept_tb"
        verbose_name_plural = "员工表"

    def __str__(self):
        return self.name


# 职级表
class RankModel(BaseModel):
    name = models.CharField("职级名称", max_length=20)
    remarks = models.CharField("备注", max_length=256)

    class Meta:
        db_table = "rank_tb"
        verbose_name_plural = "职级表"

    def __str__(self):
        return self.name


# 职位表
class PositionModel(BaseModel):
    name = models.CharField("职位名称", max_length=50)
    remarks = models.CharField("备注", max_length=256)
    dept = models.ForeignKey(DeptModel, on_delete=models.CASCADE, verbose_name="所属部门")
    rank = models.ForeignKey(RankModel, on_delete=models.CASCADE, verbose_name="所属职级")

    class Meta:
        db_table = "position_tb"
        verbose_name_plural = "职位表"

    def __str__(self):
        return self.name


# 角色表
class RoleModel(models.Model):
    name = models.CharField("角色名称", max_length=20)

    remarks = models.CharField("备注", max_length=256)
    # 创建时间
    create_time = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)

    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = "role_tb"
        verbose_name_plural = "角色表"

    def __str__(self):
        return self.name


# 菜单表
class MenuModel(BaseModel):
    name = models.CharField("菜单名称", max_length=100)
    path = models.CharField("菜单地址", max_length=100)
    icon = models.CharField("菜单图标", max_length=256)
    menu_sort = models.IntegerField(verbose_name='排序', null=True)
    remark = models.CharField(verbose_name="备注", max_length=100, null=True, blank=True)
    # 关联自己
    menu = models.IntegerField(verbose_name='上级菜单', null=True)
    role = models.ForeignKey(RoleModel, on_delete=models.PROTECT)

    class Meta:
        db_table = "menu_tb"
        verbose_name_plural = "菜单表"

    def __str__(self):
        return self.name


# 权限表
class PermissionModel(models.Model):
    name = models.CharField('权限名称', max_length=100)
    path = models.CharField('权限', max_length=256)
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="所属角色")
    # 创建时间
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'permission_tb'
        verbose_name_plural = '权限表'

    def __str__(self):
        return self.name


# 职位角色中间表
class PositionRoleModel(models.Model):
    position = models.ForeignKey(PositionModel, on_delete=models.CASCADE, verbose_name='职位')
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')
    # 创建时间
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)

    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'station_role_tb'
        verbose_name_plural = '职位角色表'

    def __str__(self):
        return '%s 拥有 %s角色' % (self.position.name, self.role.name)


# 员工表 修改完成
class UserModel(models.Model):
    STATUS_TYPE = (
        (0, '待入职'),
        (1, '实习生'),
        (2, '正式员工'),
        (3, '待离职'),
        (4, '待转正')
    )
    SEX = (
        (0, "男"),
        (1, "女")
    )
    name = models.CharField("姓名", max_length=52)
    account = models.CharField("账号", max_length=228)
    password = models.CharField("密码", max_length=256)
    mobile = models.CharField("手机号", max_length=11)
    birthday = models.DateField("生日", max_length=50)
    attribution = models.CharField("归属地", max_length=256)
    email = models.CharField("邮箱", max_length=50, unique=True)
    is_staff = models.SmallIntegerField(verbose_name="员工状态", choices=STATUS_TYPE, default=1)
    gender = models.SmallIntegerField(verbose_name="性别", choices=SEX, default=0)
    # 职位
    position = models.ForeignKey(PositionModel, on_delete=models.CASCADE, verbose_name="所属职位")
    # 创建时间
    create_time = models.DateTimeField(verbose_name="入职时间", auto_now_add=True)
    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否离职", default=False)

    class Meta:
        db_table = "user_tb"
        verbose_name_plural = "员工表"

    def __str__(self):
        return self.name


# 操作日志
class ULogModel(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="操作员工")
    dateline_time = models.DateTimeField("操作时间", auto_now_add=True)
    action = models.CharField("操作动作", max_length=256)
    path = models.CharField("操作路径", max_length=256)

    class Meta:
        db_table = "ulog_tb"
        verbose_name_plural = "操作日志"

    def __str__(self):
        return "%s员工" % (self.user.name)


# 登录日志
class ConLogModel(BaseModel):
    ip = models.CharField("登录ip", max_length=256)
    device = models.CharField("登陆设备", max_length=256)
    address = models.CharField("登陆地点", max_length=256)
    log_time = models.DateTimeField("登陆时间", auto_now_add=True)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="员工id")

    class Meta:
        db_table = "login_tb"
        verbose_name_plural = "登录日志"

    def __str__(self):
        return "%s员工的%s"%(self.user.name, self.user.mobile)


# 邮件表
class MailModel(models.Model):
    title = models.CharField('标题', max_length=20)
    address_url = models.CharField('发送地址', max_length=256)
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='接收人', related_name='接收人')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='发送人', related_name="发送人")
    content = models.CharField('发送内容', max_length=128)
    # 创建时间
    create_time = models.DateTimeField(verbose_name="发送时间", auto_now_add=True)

    # 是否删除
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'mail_tb'
        verbose_name = '邮件表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 工资条
class Salary(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    gate = models.IntegerField(verbose_name='出勤天数')
    absent = models.IntegerField(verbose_name='旷工天数')
    leave = models.IntegerField(verbose_name='请假天数')
    base_money = models.IntegerField(verbose_name='基本工资')
    station_money = models.IntegerField(verbose_name='岗位工资')

    class Meta:
        db_table = 'salary_tb'
        verbose_name_plural = '工资表'

    def __str__(self):
        return self.gate












